package algotrader.gui;

import java.text.ParseException;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JPanel;
import javax.swing.text.MaskFormatter;

import algotrader.OperadorPregao;
import algotrader.Order;

public class OrderReplacePanel extends JPanel{
	
	private static final long serialVersionUID = 1L;
	private Order order;
	private OperadorPregao operadorPregao;
	protected javax.swing.JButton entrar;
    private javax.swing.JPanel inserirOrdemPanel;
    protected JMoneyField preco;
    private javax.swing.JLabel precoLabel;
    protected javax.swing.JTextField precoStop;
    private javax.swing.JLabel precoStopLabel;
    protected javax.swing.JTextField qtd;
    private javax.swing.JLabel qtdLabel;
    protected javax.swing.JTextField qtdMin;
    private javax.swing.JLabel qtdMinLabel;
    protected javax.swing.JTextField qtdVisivel;
    private javax.swing.JLabel qtdVisivelLabel;
    private javax.swing.JLabel quantidadeLabel;
    protected javax.swing.JComboBox side;
    private javax.swing.JLabel sideLabel;
    protected javax.swing.JComboBox tipo;
    protected javax.swing.JComboBox validade;
    private javax.swing.JLabel validadeLabel;
    protected javax.swing.JFormattedTextField data;
    protected java.awt.event.ActionListener enviarListener;
	
	public OrderReplacePanel(OperadorPregao operadorPregao, Order order) {
		initComponents();
		this.order = order;
		this.operadorPregao = operadorPregao;
		this.preco.setText(this.order.getPrice()*10+""); // <<<< Refatorar isso aqui!
		this.precoStop.setText(this.order.getStopPrice()+"");
		this.qtd.setText(this.order.getQty()+"");
		this.qtdMin.setText(this.order.getMinQty()+"");
		this.qtdVisivel.setText(this.order.getMaxFloor()+"");
		this.side.setSelectedItem(this.order.getSide());
		this.side.setEnabled(false);
		this.tipo.setSelectedItem(this.order.getType());
		this.validade.setSelectedItem(this.order.getValidade());
	}
	
	private void initComponents() {
        inserirOrdemPanel = new javax.swing.JPanel();
        quantidadeLabel = new javax.swing.JLabel();
        entrar = new javax.swing.JButton("entrar");
        entrar.setName("entrar");
        tipo = new javax.swing.JComboBox();
        validadeLabel = new javax.swing.JLabel();
        validade = new javax.swing.JComboBox();
        sideLabel = new javax.swing.JLabel();
        side = new javax.swing.JComboBox();
        qtdLabel = new javax.swing.JLabel();
        qtd = new javax.swing.JTextField();
        precoLabel = new javax.swing.JLabel();
        preco = new JMoneyField();
        precoStopLabel = new javax.swing.JLabel();
        precoStop = new javax.swing.JTextField();
        qtdMinLabel = new javax.swing.JLabel();
        qtdMin = new javax.swing.JTextField();
        qtdVisivelLabel = new javax.swing.JLabel();
        qtdVisivel = new javax.swing.JTextField();
        
        enviarListener = new java.awt.event.ActionListener() {  
            public void actionPerformed(java.awt.event.ActionEvent evt) {  
                replaceOrder();
            }  
        };
        
        try {
			data = new javax.swing.JFormattedTextField (new MaskFormatter("##/##/####"));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
        inserirOrdemPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Inserir Ordem"));
        inserirOrdemPanel.setMinimumSize(new java.awt.Dimension(534, 0));
        inserirOrdemPanel.setPreferredSize(new java.awt.Dimension(534, 160));

        quantidadeLabel.setText("Tipo:");

        entrar.setText("Enviar");
        
        entrar.addActionListener(enviarListener);

        tipo.setModel(new DefaultComboBoxModel(Order.OrderType.values()));
        
        tipo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                if((Order.OrderType)tipo.getSelectedItem()==Order.OrderType.LIMIT) {
                	precoStop.setEnabled(false);
                	preco.setEnabled(true);
                } else if((Order.OrderType)tipo.getSelectedItem()==Order.OrderType.STOP_LIMIT) {
                	precoStop.setEnabled(true);
                	preco.setEnabled(true);
                } else if((Order.OrderType)tipo.getSelectedItem()==Order.OrderType.MARKET) {
                	precoStop.setEnabled(false);
                	preco.setEnabled(false);
                } else if((Order.OrderType)tipo.getSelectedItem()==Order.OrderType.MARKET_LIMIT) {
                	precoStop.setEnabled(true);
                	preco.setEnabled(false);
                }
            }
        });

        validadeLabel.setText("Validade:");

        validade.setModel(new DefaultComboBoxModel(Order.Validade.values()));
        
        validade.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	if((Order.Validade)validade.getSelectedItem()==Order.Validade.DATED) {
            		data.setEnabled(true);
            	} else {
            		data.setEnabled(false);
            	}
            		
            }
        });

        sideLabel.setText("Side:");

        side.setModel(new DefaultComboBoxModel(Order.Side.values()));

        qtdLabel.setText("Qtd.:");

        precoLabel.setText("Prc.");

        precoStopLabel.setText("Prc. Stop:");

        qtdMinLabel.setText("Qtd. Min.:");

        qtdVisivelLabel.setText("Qtd. Visivel:");
        
        precoStop.setEnabled(false);
        data.setEnabled(false);

        javax.swing.GroupLayout inserirOrdemPanelLayout = new javax.swing.GroupLayout(inserirOrdemPanel);
        inserirOrdemPanel.setLayout(inserirOrdemPanelLayout);
        inserirOrdemPanelLayout.setHorizontalGroup(
            inserirOrdemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(inserirOrdemPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(inserirOrdemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(entrar)
                    .addGroup(inserirOrdemPanelLayout.createSequentialGroup()
                        .addComponent(qtdLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(qtd, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(qtdMinLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(qtdMin, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(qtdVisivelLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(qtdVisivel, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(precoStopLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(precoStop, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(inserirOrdemPanelLayout.createSequentialGroup()
                        .addComponent(precoLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(preco, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(sideLabel)
                        .addGap(6, 6, 6)
                        .addComponent(side, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(quantidadeLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(validadeLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(validade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(data, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(58, 58, 58))
        );
        inserirOrdemPanelLayout.setVerticalGroup(
            inserirOrdemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(inserirOrdemPanelLayout.createSequentialGroup()
                .addGroup(inserirOrdemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(preco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(precoLabel)
                    .addComponent(side, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(sideLabel)
                    .addComponent(tipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(quantidadeLabel)
                    .addComponent(validade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(validadeLabel)
                    .addComponent(data, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(inserirOrdemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(qtdLabel)
                    .addComponent(qtdMinLabel)
                    .addComponent(qtdMin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(qtdVisivelLabel)
                    .addComponent(qtdVisivel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(qtd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(precoStop, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(precoStopLabel))
                .addGap(18, 18, 18)
                .addComponent(entrar)
                .addContainerGap(33, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(inserirOrdemPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(inserirOrdemPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
    }

	protected void replaceOrder() {
		this.order.setType((Order.OrderType)this.tipo.getSelectedItem());
		this.order.setTimeInForce((Order.Validade)this.validade.getSelectedItem());
		this.order.setQty(Double.valueOf(this.qtd.getText()));
		this.order.setMinQty(Double.valueOf(this.qtdMin.getText()));
		this.order.setMaxFloor(Double.valueOf(this.qtdVisivel.getText()));
		this.order.setPrice(Double.valueOf(this.preco.getText()));
		this.order.setStopPrice(Double.valueOf(this.precoStop.getText()));
		operadorPregao.askToAlterOrder(order);
	}

	

}
